﻿using Microsoft.Services.Tools.BizTalkOM;
using Microsoft.Win32;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace OrchestrationViewer
{
    public partial class OrchestrationViewer : Form
    {
        private Bitmap bmp;
        private static BizTalkInstallation bizTalkInstallation = new BizTalkInstallation();

        public OrchestrationViewer()
        {
            InitializeComponent();

            RegistryKey bizTalkKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\BizTalk Server\3.0");

            if (bizTalkKey != null)
            {
                RegistryKey bizTalkAdminKey = bizTalkKey.OpenSubKey(@"Administration");

                if (bizTalkAdminKey != null)
                {
                    bizTalkInstallation = new BizTalkInstallation
                    {
                        Server = (string)bizTalkAdminKey.GetValue("MgmtDBServer", Environment.MachineName),
                        MgmtDatabaseName = (string)bizTalkAdminKey.GetValue("MgmtDBName", "BizTalkMgmtDb")
                    };
                }
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var orchestrationNames = bizTalkInstallation.GetOrchestrationNames();

            foreach (String orchestrationName in orchestrationNames)
            {
                var orchestrationName1 = orchestrationName.Split('|');

                ToolStripMenuItem t = new ToolStripMenuItem();
                t.Name = orchestrationName1[0];
                t.Text = orchestrationName1[1];
                t.Click += new EventHandler(orchestrationsToolStripMenuItem_Click);
                orchestrationsToolStripMenuItem.DropDownItems.Add(t);
            }

            orchestrationsToolStripMenuItem.Enabled = true;
        }

        private void openOrchestration(Orchestration orchestration)
        {
            if (orchestration != null)
            {
                this.Text = "Orchestration: " + orchestration.Name;
                bmp = orchestration.GetImage();

                if (bmp != null)
                {
                    pictureBoxOrchestration.Image = bmp;
                }

                toolStripStatusLabel.Text = orchestration.Name;

                this.panel1.Focus();
            }
        }

        private void orchestrationsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ToolStripDropDownItem item = sender as ToolStripDropDownItem;

            Orchestration o = bizTalkInstallation.GetOrchestration(item.Name, item.Text);
            openOrchestration(o);
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            saveFileDialog.FileName = toolStripStatusLabel.Text;
            saveFileDialog.ShowDialog();

            // If the file name is not an empty string open it for saving.
            if (saveFileDialog.FileName != "")
            {
                if (bmp != null)
                {
                    // Saves the Image via a FileStream created by the OpenFile method.
                    System.IO.FileStream fs = (System.IO.FileStream)saveFileDialog.OpenFile();

                    bmp.Save(fs, System.Drawing.Imaging.ImageFormat.Png);

                    fs.Close();
                }
            }

        }

        private void saveToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            saveToolStripMenuItem_Click(sender, e);
        }

        private void copyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Clipboard.SetImage(bmp);
        }
    }
}
